package com.chenfan.mcn.model.dto;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.alibaba.fastjson.annotation.JSONField;
import com.chenfan.mcn.constant.SeparatorCst;
import com.chenfan.mcn.model.StarPlatformInfo;
import com.chenfan.mcn.utils.CommonUtil;
import lombok.Data;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 导入更新平台excel-失败原因导出DTO
 * @author wulg
 * @date 2023/5/16
 */
@Data
public class StarPlatformInfoOutputDTO extends StarPlatformInfoInputDTO {

    private static final long serialVersionUID = 1635737485297435352L;

    /**
     * 失败原因
     */
    @Excel(name = "失败原因" , orderNum = "100")
    private String failedReason;

    @JSONField(serialize = false)
    private StarPlatformInfo starPlatformInfo;

    /**
     * 校验系统数据匹配
     * @param separator
     */
    public void verifySysDataMatch(Map<String, String> platformMap, Map<String, List<StarPlatformInfo>> starPlatformInfoMap, String separator) {
        if(StringUtils.isNotBlank(this.getPlatformName())) {
            if(CommonUtil.containsKey(platformMap, this.getPlatformName())) {
                this.setPlatformId(platformMap.get(this.getPlatformName()));
            } else {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "所在平台录入错误，请检查后重试！"));
            }
        }
        if(StringUtils.isNotBlank(this.getPlatNickName()) && Objects.nonNull(this.getPlatformId())) {
            List<StarPlatformInfo> platformInfos = CommonUtil.getMapValue(starPlatformInfoMap, StringUtils.joinWith(SeparatorCst.PLUS, this.getPlatformId(), this.getPlatNickName()));
            boolean matchMore = false;
            if(CollectionUtils.isNotEmpty(platformInfos)) {
                if(platformInfos.size() == 1) {
                    this.setStarPlatformInfo(platformInfos.get(0));
                } else {
                    matchMore = true;
                    this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "平台昵称+所在平台匹配到多条数据，不允许更新！"));
                }
            }
            if(Objects.isNull(starPlatformInfo) && !matchMore) {
                this.setFailedReason(CommonUtil.joinWith(separator,this.getFailedReason(), "所在平台+平台昵称录入错误，请检查后重试！"));
            }
        }
    }
}
